<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/20
 * Time: 11:26
 */

namespace wodrow\tools;


class Tools
{
    /**
     * 自定义调试记录
     * @param $data
     * @param string $category
     */
    public static function log($data, $category = 'app')
    {
        file_put_contents(DATA_PATH."/app.log", date("Y-m-d H:i:s")." ".$category, FILE_APPEND);
        file_put_contents(DATA_PATH."/app.log", "\r", FILE_APPEND);
        file_put_contents(DATA_PATH."/app.log", is_array($data)?var_export($data, true):print_r($data, true), FILE_APPEND);
        file_put_contents(DATA_PATH."/app.log", "\r\r", FILE_APPEND);
    }

    /**
     * 对unicode码再进行解码
     * @param string $str
     * @return string
     */
    public static function decodeUnicode($str)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
            create_function(
                '$matches',
                'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
            ),
            $str);
    }
    
    public static function retrieve($url)
    {
        //  preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/', $url, $match);
        //  return $match[1];
        $ext = self::getExt($url);
        return str_replace('.'.$ext, '', $url);
    }
    
    public static function getExt($url)
    {
        //  $path=parse_url($url);
        //  $str=explode('.', $path['path']);
        //  return $str[1];
        return pathinfo($url, PATHINFO_EXTENSION);
    }

    /**
     * 生成目录
     * @access public
     * @param string $path 目录位置与名称
     * @return void
     * @author wodrow <wodrow451611cv@gmail.com | 1173957281@qq.com>
     */
    public static function createDir($path)
    {
        return is_dir($path) or (self::createDir(dirname($path)) and mkdir($path, 0777));
    }
}